package staff;

import java.sql.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;


public class change extends JFrame {
    private static final long serialVersionUID = -1928970409928880648L;

    JLabel jlpno = new JLabel("工      号：");
    JLabel jlname = new JLabel("姓      名：");
    JLabel jlsex = new JLabel("性      别：");
    JLabel jlbirth = new JLabel("出 生 年 月：");
    JLabel jleducation = new JLabel("年      龄：");
    JLabel jlpolitics = new JLabel("政 治 面 貌：");
    JLabel jlmarriage = new JLabel( "婚 姻 状 况：");
    JLabel jlphone = new JLabel("联 系 电 话：");
    JLabel jlsection = new JLabel("部      门：");
    JLabel jlwork = new JLabel("职      务：");

    JTextField jtpno = new JTextField("",20);
    JTextField jtname = new JTextField("",20);
    JTextField jtsex = new JTextField("",20);
    JTextField jtbirth = new JTextField("",20);
    JTextField jteducation = new JTextField("",20);
    JTextField jtpolitics = new JTextField("",20);
    JTextField jtmarriage = new JTextField("",20);
    JTextField jtphone = new JTextField("",20);
    JTextField jtsection = new JTextField("",20);
    JTextField jtwork = new JTextField("",20);

    JButton buttonchange = new JButton("修改");
    JButton buttonreturn = new JButton("返回");


    public change () {

        staff.look look1 = new staff.look();

        JPanel jppno = new JPanel();
        JPanel jpname = new JPanel();
        JPanel jpsex = new JPanel();
        JPanel jpbirth = new JPanel();
        JPanel jpeducation = new JPanel();
        JPanel jppolitics = new JPanel();
        JPanel jpmarriage = new JPanel();
        JPanel jpphone = new JPanel();
        JPanel jpsection = new JPanel();
        JPanel jpwork = new JPanel();

        JPanel jpforbutton = new JPanel(new GridLayout(1, 1));

        jppno.add(jlpno);
        jppno.add(jtpno);

        jpname.add(jlname);
        jpname.add(jtname);

        jpsex.add(jlsex);
        jpsex.add(jtsex);

        jpbirth.add(jlbirth);
        jpbirth.add(jtbirth);

        jpeducation.add(jleducation);
        jpeducation.add(jteducation);

        jppolitics.add(jlpolitics);
        jppolitics.add(jtpolitics);

        jpmarriage.add(jlmarriage);
        jpmarriage.add(jtmarriage);

        jpphone.add(jlphone);
        jpphone.add(jtphone);

        jpsection.add(jlsection);
        jpsection.add(jtsection);

        jpwork.add(jlwork);
        jpwork.add(jtwork);

        jpforbutton.add(buttonchange);
        jpforbutton.add(buttonreturn);

        buttonchange.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {

                    String xpno = jtpno.getText();
                    String xname = jtname.getText();
                    String xsex = jtsex.getText();
                    String xbirth = jtbirth.getText();
                    String xeducation = jteducation.getText();
                    String xpolitics = jtpolitics.getText();
                    String xmarriage = jtmarriage.getText();
                    String xphone = jtphone.getText();
                    String xsection = jtsection.getText();
                    String xwork = jtwork.getText();

                    Connection Test = null;
                    ResultSet res = null;
                    Statement stat = null;

                    String sql = "SELECT pno,name,xingbie,birth,age,politics,marriage,phone,section,zhiwu FROM allstaff;";
                    try {
                        Class.forName("com.mysql.cj.jdbc.Driver");

                    } catch (Exception d) {

                        System.out.println("jdbc fall");
                    }
                    try {
                        Test = DriverManager.getConnection("jdbc:mysql://localhost:3306/work", "root", "123456");
                        stat = Test.createStatement();
                        res = stat.executeQuery(sql);

                        while (res.next()) {
                            String i = res.getString(1).trim();
                            if (i.equals(jtpno.getText())) {
                                String sql2 = "UPDATE allstaff SET pno='" + xpno + "'  WHERE pno='" + jtpno.getText() + "'";
                                String sql3 = "UPDATE allstaff SET name='" + xname + "'  WHERE pno='" + jtpno.getText() + "'";
                                String sql4 = "UPDATE allstaff SET xingbie='" + xsex + "'  WHERE pno='" + jtpno.getText() + "'";
                                String sql5 = "UPDATE allstaff SET birth='" + xbirth + "'  WHERE pno='" + jtpno.getText() + "'";
                                String sql6 = "UPDATE allstaff SET age='" + xeducation + "'  WHERE pno='" + jtpno.getText() + "'";
                                String sql7 = "UPDATE allstaff SET politics='" + xpolitics + "'  WHERE pno='" + jtpno.getText() + "'";
                                String sql8 = "UPDATE allstaff SET marriage='" + xmarriage + "'  WHERE pno='" + jtpno.getText() + "'";
                                String sql9 = "UPDATE allstaff SET phone='" + xphone + "'  WHERE pno='" + jtpno.getText() + "'";
                                String sql10 = "UPDATE allstaff SET section='" + xsection + "'  WHERE pno='" + jtpno.getText() + "'";
                                String sql11 = "UPDATE allstaff SET zhiwu='" + xwork + "'  WHERE pno='" + jtpno.getText() + "'";

                                stat = Test.createStatement();

                                stat.executeUpdate(sql2);
                                stat.executeUpdate(sql3);
                                stat.executeUpdate(sql4);
                                stat.executeUpdate(sql5);
                                stat.executeUpdate(sql6);
                                stat.executeUpdate(sql7);
                                stat.executeUpdate(sql8);
                                stat.executeUpdate(sql9);
                                stat.executeUpdate(sql10);
                                stat.executeUpdate(sql11);


                                if (e.getSource() == buttonchange) {
                                    javax.swing.JOptionPane.showMessageDialog(null, "修改成功!");
                                }
                                try {
                                    stat.close();
                                    Test.close();
                                } catch (SQLException ar) {
                                    ar.printStackTrace();
                                }
                                break;
                            }
                        }
                    } catch (SQLException e1) {
                        //System.out.println("修改错误");
                        //if (e.getSource() == buttonchange) {
                            //javax.swing.JOptionPane.showMessageDialog(null, "修改格式错误，重新修改!");

                    } finally {


                        staff.look look = new staff.look();

                        System.out.println("修改操作结束");
                        try {
                            Test.close();
                        } catch (SQLException ar) {
                            ar.printStackTrace();
                        }
                    }

            }
        });
        buttonreturn.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                dispose();

            }
        });
        this.setTitle("修改教师信息");
        this.setLayout(new GridLayout(11, 1));
        this.add(jppno);
        this.add(jpname);
        this.add(jpsex);
        this.add(jpbirth);
        this.add(jpeducation);
        this.add(jppolitics);
        this.add(jpmarriage);
        this.add(jpphone);
        this.add(jpsection);
        this.add(jpwork);

        this.add(jpforbutton);
        this.setLocation(400, 300);
        this.setSize(350, 300);
        this.setVisible(true);
    }
}